package com.ht.action;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{
	private static final long serialVersionUID = 1L;

	//用户上传的文件
	private List<File> uploadFile;
	//文件名
	private List<String> uploadFileFileName;
	//文件类型
	private List<String> uploadFileContentType;
	
	public List<File> getUploadFile() {
		return uploadFile;
	}
	public void setUploadFile(List<File> uploadFile) {
		this.uploadFile = uploadFile;
	}
	public List<String> getUploadFileFileName() {
		return uploadFileFileName;
	}
	public void setUploadFileFileName(List<String> uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}
	public List<String> getUploadFileContentType() {
		return uploadFileContentType;
	}
	public void setUploadFileContentType(List<String> uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}

	@Override
	public String execute() throws Exception {
		
		   // 把上传的文件放到指定的路径下 
	       String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
	       System.out.println(path);
	       // 写到指定的路径中  
	       File file = new File(path);  
	       // 如果没有upload文件则创建
	       if (!file.exists()) {
	           file.mkdirs();  
	       }  
	       
		if (uploadFile != null) {
			//文件存放的目录
			//String dataDir = "D://upload";
			//将上传的文件从临时文件复制到指定文件
			System.out.println("========================" + uploadFile.size());
			System.out.println("===========================" + uploadFileFileName);
			for (int i = 0; i < uploadFile.size(); i++) {
				//构造要保存的文件 （文件路径，文件名）
				File savedFile = new File(path,uploadFileFileName.get(i));
				File myFile = uploadFile.get(i);
				myFile.renameTo(savedFile);
			}
			
		} else {
			return INPUT;
		}
		return SUCCESS;
	}
}
